package tr.edu.ege.seagent.hobnobwebapp;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import tr.edu.ege.seagent.hobnobontology.instance.HobNobResultSetSolution;
import tr.edu.ege.seagent.hobnobontology.instance.HobNobSPARQLQueries;

/**
 * Servlet implementation class AndroidCoordinatesOfTheLocation
 */
@WebServlet("/Coordinate")
public class AndroidCoordinatesOfTheLocation extends HttpServlet {
	private static final long serialVersionUID = 1L;
	Place place = new Place();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AndroidCoordinatesOfTheLocation() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json");
		PrintWriter out = response.getWriter();
		// Getting resourceURI as a parameter...
		String resourceURI = request.getParameter("resourceURI");
		place.setNotificationURI(resourceURI);
		// find linkedgeodata locations...
		HobNobSPARQLQueries query = new HobNobSPARQLQueries();
		List<HobNobResultSetSolution> resultSet = query
				.findLocationOfNotificationOverHttp(resourceURI);

		// get place list...
		ArrayList<String> placeList = (ArrayList<String>) getPlaces(resultSet);
		place.setPlaceURIs(placeList);

		// traverse placeURIList
		for (String placeURI : placeList) {
			List<HobNobResultSetSolution> placeResults = getPlaceResults(
					resultSet, placeURI);
			// get latitude and longitude
			getCoordinates(placeResults);
		}
		out.println(new Gson().toJson(place).toString());
	}

	/**
	 * Writes latitude and longitude of linkedgeodata resources.
	 * 
	 * @param out
	 *            writer.
	 * 
	 * @param placeResults
	 *            list of solutions about place.
	 */
	private void getCoordinates(List<HobNobResultSetSolution> placeResults) {
		// get first result to write common properties...
		HobNobResultSetSolution firstResult = placeResults.get(0);

		// latitude
		String latitude = Double.toString(firstResult.getxCoordinate()
				.asLiteral().getDouble());
		// longitude
		String longitude = Double.toString(firstResult.getyCoordinate()
				.asLiteral().getDouble());

		place.putCoordinates(latitude, longitude);

	}

	/**
	 * Retrieves the solution list of given place.
	 * 
	 * @param resultSet
	 *            result list of linkedgeodata query.
	 * @param placeURI
	 *            URI of the place which is desired to find related solutions.
	 * @return list of solutions whose location is the given place.
	 */
	private List<HobNobResultSetSolution> getPlaceResults(
			List<HobNobResultSetSolution> resultSet, String placeURI) {
		List<HobNobResultSetSolution> placeResults = new ArrayList<HobNobResultSetSolution>();
		for (HobNobResultSetSolution result : resultSet) {
			if (placeURI.equals(result.getLocation().asResource().getURI())) {
				placeResults.add(result);
			}
		}
		return placeResults;
	}

	/**
	 * Traverses the given result set and collects place URIs.
	 * 
	 * @param resultSet
	 *            result of the linkedgeodata place query.
	 * @return a list of place URIs which are not repeated.
	 */
	private List<String> getPlaces(List<HobNobResultSetSolution> resultSet) {
		List<String> placeURIList = new ArrayList<String>();
		for (HobNobResultSetSolution result : resultSet) {
			String placeURI = result.getLocation().asResource().getURI();
			if (!placeURIList.contains(placeURI)) {
				placeURIList.add(placeURI);
			}
		}
		return placeURIList;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
